package com.market.cash.ui.adapter

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.market.cash.CashRootApps
import com.market.cash.R
import com.market.cash.utils.CashRootDensityUtil
import com.market.cash.utils.adapter.CashRootRecyclerViewAdapter
import com.market.cash.utils.adapter.CashRootViewHolderHelper
import com.market.cash.utils.decoration.LinearSpacingItemDecoration
import com.market.cash.utils.dialog.CashRootCommonPopupwindow
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import org.json.JSONObject


/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/22 10:43
 * @Annotation：流程 - 联系人适配器
 */
class CashRootStepContactAdapter(private val parentView:ViewGroup, recyclerView: RecyclerView?) : CashRootRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.cash_step_contact_item) {

    /**
     * 选择联系电话监听
     */
    private var chooseMobileListsner: IChildItemSelectMobileListener? = null
    fun setOnChildItemSelectMobileListener(listener: IChildItemSelectMobileListener){
        chooseMobileListsner = listener
    }

    override fun fillData(helper: CashRootViewHolderHelper, position: Int, model: JSONObject) {
        helper.setText(R.id.textIndex,String.format(CashRootApps.app.getString(R.string.step_contact_index),position + 1))

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = CashRootDensityUtil.dp2px(16f)
            recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0, 0, topDecoration))
        }
        val adapter = CashRootStepCommonAdapter(recyclerView)
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "SELECT" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null) {
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView!!, dataList) { data ->
                        childModel.put("selectItem", data)
                        adapter.notifyItemChanged(childPosition)

                        val subRemark = "${data.optString("value")}$$${data.optString("duration")}"
                        CashRootEventHelper.addCashRootRequestEventAction(String.format(CashRootEventType.CLICK.CASHROOT_CLICK_STEP_XX_SELECT, childModel.optString("paramName")), subRemark)
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    if (chooseMobileListsner != null){
                        chooseMobileListsner!!.onChildSelectMobileClick(position,childModel)
                    }
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                dataList.add(dataArray.optJSONObject(i))
            }
        }
        adapter.data = dataList

        if (position == itemCount - 1){
            helper.setVisibility(R.id.vLine,View.GONE)
        } else {
            helper.setVisibility(R.id.vLine,View.VISIBLE)
        }
    }

    /**
     * choose Mobile
     */
    interface IChildItemSelectMobileListener{
        fun onChildSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    /**
     * SELECT
     */
    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = CashRootCommonPopupwindow(mContext)
        selectPopup.setWindowWidth(view.width + CashRootDensityUtil.dp2px(10f))
        selectPopup.setSelectItemListener(object : CashRootCommonPopupwindow.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                listener.onChildSelectClick(data!!)
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        selectPopup.show(view, parentView!!)
        selectPopup.setBrandList(dataList)
    }
}